<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>Go by Example 中文版: Context</title>
    <link rel=stylesheet href="site.css">
  </head>
  <script>
      onkeydown = (e) => {
          
          if (e.key == "ArrowLeft") {
              window.location.href = 'http-servers';
          }
          
          
          if (e.key == "ArrowRight") {
              window.location.href = 'spawning-processes';
          }
          
      }
  </script>
  <body>

    <div class="example" id="context">
      <h2><a href="./">Go by Example 中文版</a>: Context</h2>
      
      <table>
        
        <tr>
          <td class="docs">
            <p>在前面的示例中，我们研究了配置简单的 <a href="http-servers">HTTP 服务器</a>。
HTTP 服务器对于演示 <code>context.Context</code> 的用法很有用的，
<code>context.Context</code> 被用于控制 cancel。
<code>Context</code> 跨 API 边界和协程携带了：deadline、取消信号以及其他请求范围的值。</p>

          </td>
          <td class="code empty leading">
            
          
          </td>
        </tr>
        
        <tr>
          <td class="docs">
            
          </td>
          <td class="code leading">
            <a href="http://play.golang.org/p/pCfixM3AAkf"><img title="Run code" src="play.png" class="run" /></a><img title="Copy code" src="clipboard.png" class="copy" />
          <div class="highlight"><pre><span class="kn">package</span> <span class="nx">main</span>
</pre></div>

          </td>
        </tr>
        
        <tr>
          <td class="docs">
            
          </td>
          <td class="code leading">
            
          <div class="highlight"><pre><span class="kn">import</span> <span class="p">(</span>
    <span class="s">&quot;fmt&quot;</span>
    <span class="s">&quot;net/http&quot;</span>
    <span class="s">&quot;time&quot;</span>
<span class="p">)</span>
</pre></div>

          </td>
        </tr>
        
        <tr>
          <td class="docs">
            
          </td>
          <td class="code leading">
            
          <div class="highlight"><pre><span class="kd">func</span> <span class="nx">hello</span><span class="p">(</span><span class="nx">w</span> <span class="nx">http</span><span class="p">.</span><span class="nx">ResponseWriter</span><span class="p">,</span> <span class="nx">req</span> <span class="o">*</span><span class="nx">http</span><span class="p">.</span><span class="nx">Request</span><span class="p">)</span> <span class="p">{</span>
</pre></div>

          </td>
        </tr>
        
        <tr>
          <td class="docs">
            <p><code>net/http</code> 机制为每个请求创建了一个 <code>context.Context</code>，
并且可以通过 <code>Context()</code> 方法获取并使用它。</p>

          </td>
          <td class="code leading">
            
          <div class="highlight"><pre>    <span class="nx">ctx</span> <span class="o">:=</span> <span class="nx">req</span><span class="p">.</span><span class="nx">Context</span><span class="p">()</span>
    <span class="nx">fmt</span><span class="p">.</span><span class="nx">Println</span><span class="p">(</span><span class="s">&quot;server: hello handler started&quot;</span><span class="p">)</span>
    <span class="k">defer</span> <span class="nx">fmt</span><span class="p">.</span><span class="nx">Println</span><span class="p">(</span><span class="s">&quot;server: hello handler ended&quot;</span><span class="p">)</span>
</pre></div>

          </td>
        </tr>
        
        <tr>
          <td class="docs">
            <p>等待几秒钟，然后再将回复发送给客户端。
这可以模拟服务器正在执行的某些工作。
在工作时，请密切关注 context 的 <code>Done()</code> 通道的信号，
一旦收到该信号，表明我们应该取消工作并尽快返回。</p>

          </td>
          <td class="code leading">
            
          <div class="highlight"><pre>    <span class="k">select</span> <span class="p">{</span>
    <span class="k">case</span> <span class="o">&lt;-</span><span class="nx">time</span><span class="p">.</span><span class="nx">After</span><span class="p">(</span><span class="mi">10</span> <span class="o">*</span> <span class="nx">time</span><span class="p">.</span><span class="nx">Second</span><span class="p">):</span>
        <span class="nx">fmt</span><span class="p">.</span><span class="nx">Fprintf</span><span class="p">(</span><span class="nx">w</span><span class="p">,</span> <span class="s">&quot;hello\n&quot;</span><span class="p">)</span>
    <span class="k">case</span> <span class="o">&lt;-</span><span class="nx">ctx</span><span class="p">.</span><span class="nx">Done</span><span class="p">():</span>
</pre></div>

          </td>
        </tr>
        
        <tr>
          <td class="docs">
            <p>context 的 <code>Err()</code> 方法返回一个错误，
该错误说明了 <code>Done</code> 通道关闭的原因。</p>

          </td>
          <td class="code leading">
            
          <div class="highlight"><pre>        <span class="nx">err</span> <span class="o">:=</span> <span class="nx">ctx</span><span class="p">.</span><span class="nx">Err</span><span class="p">()</span>
        <span class="nx">fmt</span><span class="p">.</span><span class="nx">Println</span><span class="p">(</span><span class="s">&quot;server:&quot;</span><span class="p">,</span> <span class="nx">err</span><span class="p">)</span>
        <span class="nx">internalError</span> <span class="o">:=</span> <span class="nx">http</span><span class="p">.</span><span class="nx">StatusInternalServerError</span>
        <span class="nx">http</span><span class="p">.</span><span class="nx">Error</span><span class="p">(</span><span class="nx">w</span><span class="p">,</span> <span class="nx">err</span><span class="p">.</span><span class="nx">Error</span><span class="p">(),</span> <span class="nx">internalError</span><span class="p">)</span>
    <span class="p">}</span>
<span class="p">}</span>
</pre></div>

          </td>
        </tr>
        
        <tr>
          <td class="docs">
            
          </td>
          <td class="code leading">
            
          <div class="highlight"><pre><span class="kd">func</span> <span class="nx">main</span><span class="p">()</span> <span class="p">{</span>
</pre></div>

          </td>
        </tr>
        
        <tr>
          <td class="docs">
            <p>跟前面一样，我们在 <code>/hello</code> 路由上注册 handler，然后开始提供服务。</p>

          </td>
          <td class="code">
            
          <div class="highlight"><pre>    <span class="nx">http</span><span class="p">.</span><span class="nx">HandleFunc</span><span class="p">(</span><span class="s">&quot;/hello&quot;</span><span class="p">,</span> <span class="nx">hello</span><span class="p">)</span>
    <span class="nx">http</span><span class="p">.</span><span class="nx">ListenAndServe</span><span class="p">(</span><span class="s">&quot;:8090&quot;</span><span class="p">,</span> <span class="kc">nil</span><span class="p">)</span>
<span class="p">}</span>
</pre></div>

          </td>
        </tr>
        
      </table>
      
      <table>
        
        <tr>
          <td class="docs">
            <p>后台运行服务器。</p>

          </td>
          <td class="code leading">
            
          <div class="highlight"><pre><span class="gp">$</span> go run context-in-http-servers.go <span class="p">&amp;</span>
</pre></div>

          </td>
        </tr>
        
        <tr>
          <td class="docs">
            <p>模拟客户端发出 <code>/hello</code> 请求，
在服务端开始处理后，按下 Ctrl+C 以发出取消信号。</p>

          </td>
          <td class="code">
            
          <div class="highlight"><pre><span class="gp">$</span> curl localhost:8090/hello
<span class="go">server: hello handler started</span>
<span class="go">^C</span>
<span class="go">server: context canceled</span>
<span class="go">server: hello handler ended</span>
</pre></div>

          </td>
        </tr>
        
      </table>
      
      
      <p class="next">
        下一个例子: <a href="spawning-processes">生成进程</a>
      </p>
      
      <p class="footer">
        <a href="https://twitter.com/mmcgrana">@mmcgrana</a> 编写 | <a href="https://github.com/gobyexample-cn">gobyexample-cn</a> 翻译 | <a href="https://github.com/gobyexample-cn/gobyexample/issues">反馈</a> | <a href="https://github.com/gobyexample-cn/gobyexample">源码</a> | <a href="https://github.com/mmcgrana/gobyexample#license">license</a>      </p>
      </p>
    </div>
    <script>
      var codeLines = [];
      codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import (\u000A    \"fmt\"\u000A    \"net/http\"\u000A    \"time\"\u000A)\u000A');codeLines.push('func hello(w http.ResponseWriter, req *http.Request) {\u000A');codeLines.push('    ctx := req.Context()\u000A    fmt.Println(\"server: hello handler started\")\u000A    defer fmt.Println(\"server: hello handler ended\")\u000A');codeLines.push('    select {\u000A    case \x3C-time.After(10 * time.Second):\u000A        fmt.Fprintf(w, \"hello\\n\")\u000A    case \x3C-ctx.Done():\u000A');codeLines.push('        err := ctx.Err()\u000A        fmt.Println(\"server:\", err)\u000A        internalError := http.StatusInternalServerError\u000A        http.Error(w, err.Error(), internalError)\u000A    }\u000A}\u000A');codeLines.push('func main() {\u000A');codeLines.push('    http.HandleFunc(\"/hello\", hello)\u000A    http.ListenAndServe(\":8090\", nil)\u000A}\u000A');codeLines.push('');codeLines.push('');
    </script>
    <script src="site.js" async></script>
  </body>
</html>
